import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/31 17:29
 */
public class JdbcDelete {
    public static void main(String[] args) throws SQLException {
        /**
         * 创建数据源
         */
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("001110");
        //建立连接
        Connection connection = dataSource.getConnection();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入删除的id: ");
        int id = sc.nextInt();

        //拼装sql语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        //执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret= "+ret);
        //回收
        statement.close();
        connection.close();
    }
}
